﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hjp.FrameWork.Common;
using System.IO;
using Wjtour.Common;

namespace Wjtour.Web.Admin.fun
{
    public partial class upload : BasePage
    {
        protected FileUpload file;
        protected Button btnUpload;
        protected string tag;

        protected void Page_Load(object sender, EventArgs e)
        {
            btnUpload.Click += new EventHandler(btnUpload_Click);
            tag = Request["tag"];
        }

        void btnUpload_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(file.FileName))
            {
                Utils.Alert(this.Page, "请选择要上传的文件！");
                return;
            }
            string fileExt = Path.GetExtension(file.FileName);
            string allowUploadType = "";
            if (!((BasePage)this.Page).CheckFileAllowUpload(fileExt, out allowUploadType))
            {
                Utils.Alert(this.Page, "系统只允许上传" + allowUploadType + "的文件！");
                return;
            }

            string imgpath = string.Empty;

            try
            {
                //上传目录
                string uploadDir = ((BasePage)this.Page).GetUploadDirectory("/uploads/", tag, out imgpath);
                //string guid = Guid.NewGuid().ToString();
                //string filename = Server.MapPath("../../Temp/" + guid + ".xls");
                string filename = uploadDir + "\\" + file.FileName;
                imgpath += "/" + file.FileName;
                if (System.IO.File.Exists(filename)) System.IO.File.Delete(filename);
                //Utility.CutForCustom(file.PostedFile, filename, Utils.StrToInt(width, 300), Utils.StrToInt(height, 300), 95);
                file.SaveAs(filename);
            }
            catch (Exception)
            {
                Utils.Alert(this.Page, "文件文件出错，文件大小超过限制或网络原因！");
                return;
            }

            Utils.ExecScript(this.Page, "back('/" + imgpath.TrimStart('/') + "');");
        }
    }
}